package com.reservation.entity;

import javax.persistence.*;
import javax.validation.constraints.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 商品实体类
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "products")
public class Product extends BaseEntity {

    @NotBlank(message = "商品名称不能为空")
    @Size(max = 100, message = "商品名称长度不能超过100个字符")
    @Column(name = "name", nullable = false, length = 100)
    private String name;

    @NotNull(message = "商品价格不能为空")
    @DecimalMin(value = "0.01", message = "商品价格必须大于0")
    @Digits(integer = 10, fraction = 2, message = "价格格式不正确")
    @Column(name = "price", nullable = false, precision = 12, scale = 2)
    private BigDecimal price;

    @NotBlank(message = "商品图片不能为空")
    @Size(max = 500, message = "图片URL长度不能超过500个字符")
    @Column(name = "image", nullable = false, length = 500)
    private String image;

    @NotNull(message = "库存数量不能为空")
    @Min(value = 0, message = "库存数量不能为负数")
    @Column(name = "stock", nullable = false)
    private Integer stock;

    @Size(max = 500, message = "商品描述长度不能超过500个字符")
    @Column(name = "description", length = 500)
    private String description;

    @Column(name = "is_active", nullable = false)
    private Boolean isActive = true;

    /**
     * 减少库存
     */
    public void decreaseStock(Integer quantity) {
        if (this.stock < quantity) {
            throw new IllegalArgumentException("库存不足");
        }
        this.stock -= quantity;
    }

    /**
     * 增加库存
     */
    public void increaseStock(Integer quantity) {
        this.stock += quantity;
    }

    /**
     * 检查库存是否充足
     */
    public boolean hasEnoughStock(Integer quantity) {
        return this.stock >= quantity;
    }
}
